package com.aimuceliang.aimuceliang.green_dao;

import android.content.Context;
import android.util.Log;

import java.util.List;

/**
 * Created by ziabo on 2017/8/16.
 * 笼称的DaoUtils
 */

public class LongChengDaoUtils {
    private static final String TAG = LongChengDaoUtils.class.getSimpleName();
    private DaoManager mManager;

    public LongChengDaoUtils(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 完成meizi记录的插入，如果表未创建，先创建Meizi表
     *
     * @param longCheng
     * @return
     */
    public boolean insertLongCheng(LongCheng longCheng) {
        boolean flag = false;
        flag = mManager.getDaoSession().getLongChengDao().insert(longCheng) != -1;
        Log.i(TAG, "insert longCheng :" + flag + "-->" + longCheng.toString());
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     *
     * @param longChengs
     * @return
     */
    public boolean insertMultLongCheng(final List<LongCheng> longChengs) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (LongCheng longcheng : longChengs) {
                        mManager.getDaoSession().insertOrReplace(longcheng);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     *
     * @param longCheng
     * @return
     */
    public boolean updateLongCheng(LongCheng longCheng) {
        boolean flag = false;
        try {
            mManager.getDaoSession().update(longCheng);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     *
     * @param longCheng
     * @return
     */
    public boolean deleteLongCheng(LongCheng longCheng) {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(longCheng);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     *
     * @return
     */
    public boolean deleteAll() {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(LongCheng.class);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 根据主键id查询记录
     *
     * @param key
     * @return
     */
    public LongCheng queryLongChengById(long key) {
        return mManager.getDaoSession().load(LongCheng.class, key);
    }

    public void closeDao() {
        mManager.closeConnection();
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<LongCheng> queryAllLongCheng(){
        return mManager.getDaoSession().loadAll(LongCheng.class);
    }
}
